package com.example.charlotte.snakegame;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by Charlotte on 2018/12/12.
 */

public class DBManager {
    private SQLiteDatabase db;
    private DBHelper dbHelper;

    public DBManager(Context context) {
        dbHelper=new DBHelper(context);
        db=dbHelper.getWritableDatabase();
    }

    /**
     * 插入数据
     * @param user
     * @return
     */
    public boolean insert(User user){
        try {
            ContentValues values=new ContentValues();
            values.put("username",user.getUserName());
            values.put("password",user.getPassword());
            db.insert("users",null,values);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 添加数据
     * @param user
     * @return
     */
    public boolean add(User user){
        try{
            String sql="insert into users(username,password) values(?,?)";
            Object[] bindArgs={user.getUserName(),user.getPassword()};
            db.execSQL(sql,bindArgs);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }



}
